//
//  UndeliveredOrderViewController.m
//  FactorySale
//
//  Created by 李 on 2020/1/1.
//  Copyright © 2020 主事丫环. All rights reserved.
//  未发货订单

#import "UndeliveredOrderViewController.h"
#import "WeiFaHuoListCell.h"// 未发货列表cell
#import "WeiFaHuoOrderListModel.h"// 未发货列表model
#import "ShengChengFaHuoDanViewController.h"// 生成发货单控制器
#import "OrderDetailViewController.h"// 订单详情控制器

@interface UndeliveredOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *listTableView; //列表
@property (nonatomic, strong) NSMutableArray *listArray;

@end

static NSString *const WeiFaHuoListCellID = @"WeiFaHuoListCell";

@implementation UndeliveredOrderViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self requestUnSendPartsOrders];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Main_BackGroundColor;
    [self configViews];
}

-(void)configViews{
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([WeiFaHuoListCell class]) bundle:nil] forCellReuseIdentifier:WeiFaHuoListCellID];
    [self.view addSubview:self.listTableView];
}
-(void)requestUnSendPartsOrders{
    NSDictionary *param = @{
        @"id":NOTNIL(self.id)//种类id
    };
    [self.listArray removeAllObjects];
    [NetWorkTool POST:getUnSendPartsOrders param:param success:^(id dic) {
        self.listArray = [WeiFaHuoOrderListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.listTableView reloadData];
    } other:^(id dic) {
        [self.listTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.listTableView reloadData];
    } needUser:YES];
}

- (UITableView *)listTableView {
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, KHEIGHT-kNaviHeight-10-kTabbarHeight) style:UITableViewStylePlain];
        _listTableView.hidden = NO;
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _listTableView.estimatedSectionHeaderHeight = 0;
        _listTableView.estimatedSectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = Main_BackGroundColor;
        _listTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:CGRectMake(0, 10, ScreenW, SCREEN_HEIGHT-kNaviHeight-45-10-10-kTabbarHeight) image:imgname(@"订单空界面") viewClick:^{
            
        }];
        UILabel * contentLabel;
        if (contentLabel) {
            [contentLabel removeFromSuperview];
        }
        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-kTabbarHeight-200, ScreenW, 20)];
        contentLabel.text = @"暂无订单信息";
        contentLabel.textColor = [UIColor colorWithHexString:@"#B78030"];
        contentLabel.textAlignment = YES;
        contentLabel.font = [UIFont boldSystemFontOfSize:13];
        [_listTableView.placeHolderView addSubview:contentLabel];
        
    }
    return _listTableView;
}

#pragma mark UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeiFaHuoListCell *cell = [tableView dequeueReusableCellWithIdentifier:WeiFaHuoListCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WeiFaHuoOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    cell.model = model;
    [cell.xiangQingBtn addTarget:self action:@selector(xiangQingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.faHuoDanBtn addTarget:self action:@selector(faHuoDanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 171;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WeiFaHuoOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    
    OrderDetailViewController *vc = [[OrderDetailViewController alloc]init];
    vc.id = self.id;
    vc.orderId = model.id;
    vc.isSubFactory = model.isSubFactory;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)xiangQingBtnClick:(UIButton *)button{
    WeiFaHuoListCell *cell = (WeiFaHuoListCell *)[[button superview] superview];
    NSIndexPath *indexPath = [_listTableView indexPathForCell:cell];
    WeiFaHuoOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    
    OrderDetailViewController *vc = [[OrderDetailViewController alloc]init];
    vc.id = self.id;
    vc.orderId = model.id;
    vc.isSubFactory = model.isSubFactory;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)faHuoDanBtnClick:(UIButton *)button{
    WeiFaHuoListCell *cell = (WeiFaHuoListCell *)[[button superview] superview];
    NSIndexPath *indexPath = [_listTableView indexPathForCell:cell];
    WeiFaHuoOrderListModel *model  = [self.listArray safeObjectAtIndex:indexPath.section];
    
    ShengChengFaHuoDanViewController *vc = [[ShengChengFaHuoDanViewController alloc]init];
    vc.id = self.id;
    vc.orderId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
